﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Kokomo.PeInspector.ObjectGraphs
{
	abstract class ElementTemplate
	{
		public abstract bool Inspect(IEnumerable enumerable);
	}
	class ElementTemplate<TElement> : ElementTemplate
	{
		public NodeTemplate<TElement> Template { get; private set; }
		public int Index { get; private set; }

		public ElementTemplate(
			int index,
			NodeTemplate<TElement> template
			)
		{
			if (template == null) throw new ArgumentNullException("template");
			if (index < 0) throw new ArgumentOutOfRangeException("index", index, "Index must be >= 0");

			this.Template = template;
			this.Index = index;
		}

		public override bool Inspect(IEnumerable enumerable)
		{
			IEnumerable<object> objectEnumerable = enumerable.OfType<object>();
			if (this.Index >= objectEnumerable.Count()) return false;

			object element = objectEnumerable.ElementAt(this.Index);

			return this.Template.Inspect(element);
		}
	}
}
